package com.shiguiwu.springmybatis.javabase.encrypt;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import lombok.extern.slf4j.Slf4j;

import java.util.Base64;

import sun.security.rsa.RSAUtil;

import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 加密
 * @author: stone
 * @date: Created by 2022/9/17 18:22
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.encrypt
 */
@Slf4j
public class EncryptTests {

    public static final String PUBLIC_KEY = "public_key";

    public static final String PRIVATE_KEY = "private_key";

    public static void main(String[] args) {
        Map<String, String> map = generateRasKey();

        log.error("密码 ===> {}", map.toString());
    }


    //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXhSrBK72Ro8r3I0Vu5sSxtxHOnT0sYET01ssJUGPLxY4Gba4gOc9PZN03yCCINW3+DPTaR9VMERISdAGrliSOliBfD2+fYNsjYnpE3N4q2RS1dRXPr5c65AFWq/K7/9kRpr/OcJpRVEDL5RS3yvmMOUBXEJgwYviWDJrAKrH1NQIDAQAB
    //MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCa/7DHje+C1VKqT6ojvI4P03klWVUk7PS3v85wTbPPs2fsmEtt7wdx0O4CXjpLAmJHbpCZJdmgKvD5Zl9d45J/mlxhiIcWsyYbEZBrG/TWiBRvGvtkA+W/P0tCNafaPhMM2cMMP2df4XYekcmanTd7eE5ycIRtrNSBdI4B9lsXwwIDAQAB
    //MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJeFKsErvZGjyvcjRW7mxLG3Ec6dPSxgRPTWywlQY8vFjgZtriA5z09k3TfIIIg1bf4M9NpH1UwREhJ0AauWJI6WIF8Pb59g2yNiekTc3irZFLV1Fc+vlzrkAVar8rv/2RGmv85wmlFUQMvlFLfK+Yw5QFcQmDBi+JYMmsAqsfU1AgMBAAECgYAe01vIYvK1L4J6VvWoQ1Ce6yS3SSwJ3CgLO6ug4Hb9HNrxGd1D9x7jAx0yXQepTl1rQw2JXvXY/NwVXv57/xa+jxVY2CYa4y5RYTEpLcM5xVQfeO7h9F1v9Th5mCi1WYRVYXb0/802NvREmlHsFwt2OD3hCnmhrwq1mV+r8w/sPQJBANRQZfQVS43FbotfOqTDQrVv4yPn+Jx1uD519LDYotQNj/+oP6z77JxK+JcGdFrfsmx9he8PfLSMtnCABLlpoMcCQQC2snacYYvOwr/j0z4BG28leEypV0H6bnqWykl88vBTy65kyUmgujbxA8rsFbSRWHpIfKwROEmHNNoPKu5i8TYjAkEApOzTVfjY2Qc96PwPlP+tKT4CBtlisT9s4PBGJL4UNn9WlbU/yYNk+L/wPXkhF5pfgpE7Ykn4iFQqQFfh05nu7wJAFwVR0M6x0THVAB4kF/ue+7e24TevhqDZLZr7u5TZF5X4uZZrgqc0ORZjQXBSumXNssiS3fCbwz+zommkdC80xQJBAMQkbEd6WsdWW5+wlm1rhNeDXdWj9RoHbAERja/NJJzJAmOPkJW5gRY/L/be/WqxZopxpUMj7ATes40Mp6sUDdM=
    public static Map<String, String> generateRasKey() {
        Map<String, String> rs = new HashMap<>(4);
        //第一种
        //KeyPair keyPair = SecureUtil.generateKeyPair("RSA");
        //
        //rs.put(PUBLIC_KEY, new String(Base64.encodeBase64(keyPair.getPublic().getEncoded())));//AQAB
        //
        //rs.put(PRIVATE_KEY, new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded())));

        //第二种方式
        // 初始化RSA工具，生成密钥对
        RSA rsa = new RSA();
        // 获取公钥
        String pubKey = rsa.getPublicKeyBase64();
        System.out.println("pubKey:" + pubKey);
        // 获取私钥
        String priKey = rsa.getPrivateKeyBase64();
        System.out.println("priKey:" + priKey);

        rs.put(PUBLIC_KEY, pubKey);
        rs.put(PRIVATE_KEY, priKey);


        return rs;
    }

    public static final String pub_key ;
    public static final String pri_key ;

    static {
        Map<String, String> map = generateRasKey();
        pub_key = map.get(PUBLIC_KEY);
        pri_key = map.get(PRIVATE_KEY);
    }


    // 初始化RSA工具并设置公钥
    public static RSA pubRsa = new RSA(null, pub_key);

    // 初始化RSA工具并设置私钥
    public static RSA priRsa = new RSA(pri_key, null);

    // 初始化自定义密钥对RSA工具，但是一般不这么用，因为公私钥需要分离
    //public static RSA Rsa = new RSA(pri_key, pub_key);

    public static RSA rsa = new RSA();//这样用

    //公钥加密,加密：用公钥加密，私钥解密。用于向公钥所有者发布信息,这个信息可能被他人篡改,但是无法被他人获得。
    public static String encryptPublicKey(String source, String publicKey) {
        // 初始化RSA工具并设置公钥
        //RSA pubRsa = new RSA(null, publicKey);

        return pubRsa.encryptBase64(source, KeyType.PublicKey);
        //byte[] encrypt = rsa.encrypt(StrUtil.bytes(source, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        //return StrUtil.str(encrypt, CharsetUtil.CHARSET_UTF_8);
    }

    //签名：使用私钥加密，公钥解密。用于让所有公钥所有者验证私钥所有者的身份并且用来防止私钥所有者发布的内容被篡改，但是不用来保证内容不被他人获得。
    public static String encryptPrivateKey(String source, String privateKey) {
        //RSA priRsa = new RSA(privateKey, null);
        return priRsa.encryptBase64(source, KeyType.PrivateKey);

    }




    /**
     * 字节数组转Base64编码
     *
     * @param bytes 字节数组
     * @return Base64编码
     */
    private static String bytesToBase64(byte[] bytes) {
        byte[] encodedBytes = Base64.getEncoder().encode(bytes);
        return new String(encodedBytes, StandardCharsets.UTF_8);
    }


    /**
     * Base64编码转字节数组
     *
     * @param base64Str Base64编码
     * @return 字节数组
     */
    private static byte[] base64ToBytes(String base64Str) {
        byte[] bytes = base64Str.getBytes(StandardCharsets.UTF_8);
        return Base64.getDecoder().decode(bytes);
    }


    //原生


}
